// pages/addres/index.js
import {applyInfo,applyDetail} from '../../../api/apply'
const app = getApp().globalData

Page({
  data: {
    rates: ['差评', '中评', '好评'],
    rateIdx: 0,
    evaluate: '',
    content: '',
    pid: '',
    tagList: [],
    serverList: [],
    piclist: [],


    hasDis: false
  },
 
  onLoad: function (options) {
   this.data.pid = options.id
  // var apply = JSON.parse(options.apply);
  //  console.log(apply)
   console.log(options);
   this.getInfo(options.id);
  },
  getInfo(id){
    applyDetail( id ).then((res) => {
      console.log(res.datas.scores);
      // res.datas.scores
      // res.datas.evaluate
    this.setData({ 
      rateIdx: res.datas.scores,
      evaluate: res.datas.evaluate,
      hasDis: res.datas.scores > 0
     })
     console.log(this.data.rateIdx);
  })
  },
  delitem(e) {
    let temp = this.data.piclist
    const idx = e.currentTarget.dataset.index
    temp.splice(idx, 1)
    this.setData({
      piclist: temp
    })
  },
  addRes() {
    var _this=this;
    wx.chooseImage({
      count: 9 - this.data.piclist.length, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        _this.setData({
          piclist: _this.data.piclist.concat(tempFilePaths)
        })
      }
    })
  },
  setTag(e) {
    const { k } = e.currentTarget.dataset
    this.setData({ content: k})
  },
  setIdx(e) {
    if (this.data.hasDis) {
      return
    }
    const { idx } = e.currentTarget.dataset
    this.setData({ rateIdx: idx * 1 + 1 })
  },
  onSwitchChange({ detail }) {
    this.setData({ 
      nm: detail,
    });
  },
  //applyInfo
  submit() {
    if (this.data.rateIdx == 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择评分'
      })
      return
    }
    console.log(this.data.rateIdx);
    applyInfo({scores: this.data.rateIdx, id:this.data.pid,
      evaluate: this.data.evaluate
    }).then((res) => {
       console.log(res); 
       this.getInfo(this.data.pid) ;   
    })
    // applyInfo({id:this.data.pid,scores: 99, evaluate:'无评论'}).then((res) => {
          
    // })
  },
  submit1() {
    if (this.data.rateIdx == 0) {
      wx.showToast({
        icon: 'none',
        title: '请选择评分'
      })
      return
    }
    if (this.data.piclist.length) {
      wx.showLoading({
        title: '上传中...',
        mask: true
      })
      this.data.piclist.forEach((it, idx) => {
        this.uploadFile(it)
      })
    } else {
      this.subDataForm()
    }
  },
  uploadFile(it) {
    wx.uploadFile({
      url: 'https://www..com/mini/' + 'index/uploads' ,
      filePath: it,
      name: "image",
      header: {
        "Content-Type": "multipart/form-data",
        "X-TOKEN": app.x_tk
      },
      formData: {
      },
      success:(res) => {
        const t = JSON.parse(res.data)
        this.data.serverList.push(t.datas.path)
        if (this.data.serverList.length == this.data.piclist.length) {
          this.subDataForm()
        }
      }
    })
},
  subDataForm() {
    let p = {
      InfoID: this.data.pid,
      CommentRank: this.data.rateIdx,
      CommentContent: this.data.content,
      IsNm: this.data.nm ? 1 : 0,
      CommentPicture: JSON.stringify(this.data.serverList),
      type: 0
    }
    addPl(p).then((res) => {
      wx.showToast({
        icon: 'success',
        title: res.message,
      })
      app.isReload = true
      setTimeout(() => {
        wx.navigateBack()
      }, 1000);
    })
  },
  conInput(e) {
    this.setData({
      content: e.detail.value
    })
  },
  
})